using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using System.IO;

//using UnityEngine.AddressableAssets;

using KFGame;

public class TableManager : MonoBehaviour
{


    /// <summary>
    /// 文件路径
    /// </summary>
    public static string path = "Build/Data";

    /// <summary>
    /// 文件名
    /// </summary>
    public static string dataFiles = "data.bytes";

    /// <summary>
    /// 数据表管理入口对象
    /// </summary>
    public KFTable.Table table;

    /// <summary>
    /// 初始化数据
    /// </summary>
    public void inits()
    {
        KFTable.Table tab = new KFTable.Table();
        LoadTableByNameFromSreamingAssets(tab, dataFiles);
        table = tab;
    }

    static TableManager tableManager;
    public static TableManager Instantiation()
    {
        if (!tableManager)
        {
            tableManager = new TableManager();
            tableManager.inits();
        }

        return tableManager;

    }



    /// <summary>
    /// Resources 文件夹中
    /// 通过资源加载器文件
    /// </summary>
    /// <param name="tab"></param>
    /// <param name="tablename"></param>
    public static void LoadTableFromResources(KFTable.Table tab, string tablename)
    {
        TextAsset ta = Resources.Load<TextAsset>(path + "/" + tablename) as TextAsset;

        using (var stream = new MemoryStream(ta.bytes))
        {
            stream.Position = 0;

            var reader = new tabtoy.TableReader(stream);
            try
            {
                tab.Deserialize(reader);
            }
            catch (System.Exception e)
            {
                Debug.LogError(e);
                throw;
            }
        }
    }


    /// <summary>
    /// AddressableAssets 资源工具中加载
    /// </summary>
    /// <param name="tab"></param>
    /// <param name="table"></param>
    /// <returns></returns>
    public static async void AddressablesLoad(tabtoy.ITableSerializable tab, string table)
    {
        // var ta = await Addressables.LoadAssetAsync<TextAsset>("Assets/Data/tabledata.bytes").Task;


        // using (var stream = new MemoryStream(ta.bytes))
        // {
        //     stream.Position = 0;

        //     var reader = new tabtoy.TableReader(stream);

        //     try
        //     {
        //         tab.Deserialize(reader);
        //     }
        //     catch (System.Exception e)
        //     {
        //         Debug.LogError(e);
        //         throw;
        //     }
        // }
    }



    /// <summary>
    ///  streamingAssets 文件夹中加载
    ///   返回读取对象
    /// </summary>
    /// <param name="tableName"></param>
    /// <returns></returns>
    public static tabtoy.TableReader LoadTableReaderFromSreamingAssets(string filename)
    {

        using (var stream = new FileStream(Application.streamingAssetsPath + "/" + path + "/" + filename, FileMode.Open))
        {
            stream.Position = 0;
            var reader = new tabtoy.TableReader(stream);
            return reader;
        }
    }

    /// <summary>
    /// streamingAssets 文件夹中加载
    /// 读取指定名字的表, 可根据实际需求调整该函数适应不同加载数据来源
    /// </summary>
    /// <param name="tab"></param>
    /// <param name="filename"></param>
    public static void LoadTableByNameFromSreamingAssets(KFTable.Table tab, string filename)
    {
        using (var stream = new FileStream(Application.streamingAssetsPath + "/" + path + "/" + filename, FileMode.Open))
        {
            stream.Position = 0;

            var reader = new tabtoy.TableReader(stream);
            try
            {
                tab.Deserialize(reader);
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                throw;
            }
        }
    }
}
